package com.ceac.kronos.ejb.entitybeans;

import com.ceac.kronos.ejb.entitybeans.Category;

import java.io.Serializable;

import java.sql.Timestamp;

import java.util.Date;
import java.util.List;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.JoinTable;
import javax.persistence.ManyToMany;
import javax.persistence.NamedQueries;
import javax.persistence.NamedQuery;
import javax.persistence.OneToMany;
import javax.persistence.OneToOne;
import javax.persistence.QueryHint;
import javax.persistence.SequenceGenerator;
import javax.persistence.Table;
import javax.persistence.TableGenerator;

@TableGenerator(table = "SEQUENCES", schema = "kronos",
                pkColumnName = "SEQUENCE_NAME", valueColumnName = "SEQUENCE_COUNT", pkColumnValue="DEFAULT",
                allocationSize = 1, name="defaultSequence")
@Entity
@NamedQueries( { @NamedQuery(name = "Participant.findAll", query = "select o from Participant o"),
        @NamedQuery(name = "Participant.findParticipation", query = "select o from Participant o where o.person = :person and o.race=:race",
            hints = { @QueryHint(name = "eclipselink.jdbc.max-rows", value = "1") }),
        @NamedQuery(name = "Participant.findByBibsNumber", query = "select o from Participant o where o.race = :race and o.bibsNumber=:bibsNumber") })
@Table(name = "PARTICIPANTS")
public class Participant implements Serializable {
    @Column(name="BIBS_NUMBER", nullable = false)
    private Integer bibsNumber;

    @Column(name="CREATED_BY")
    private Integer createdBy;
    @Column(name="CREATION_DATE")
    private Timestamp creationDate;
    @Column(name="FINISH_TIME")
    private Timestamp finishTime;
    @Column(name="LAST_UPDATED_BY")
    private Integer lastUpdatedBy;
    @Column(name="LAST_UPDATE_DATE")
    private Timestamp lastUpdateDate;
    @Id
    @Column(name="PARTICIPANT_ID", nullable = false)
    @GeneratedValue(generator="defaultSequence")
    private Integer participantId;

    @ManyToMany(targetEntity = Category.class)
    @JoinTable(name = "PARTICIPANTS_CATEGORIES", schema = "kronos",
               joinColumns = { @JoinColumn(name = "PARTICIPANT_ID",
                    referencedColumnName = "PARTICIPANT_ID") })
    private List<Category> categories;
    @OneToOne(fetch = FetchType.EAGER, targetEntity = Person.class)
    @JoinColumn(name = "PERSON_ID", referencedColumnName = "PERSON_ID")
    private Person person;
    @OneToOne(fetch = FetchType.EAGER, targetEntity = Race.class)
    @JoinColumn(name = "RACE_ID", referencedColumnName = "RACE_ID")
    private Race race;
    @OneToMany(targetEntity = Time.class, mappedBy = "participant")
    private List<Time> times;

    public Participant() {
        super();
        this.createdBy = 0;
        this.lastUpdatedBy = 0;
      this.creationDate = new Timestamp((new Date()).getTime());
      this.lastUpdateDate = new Timestamp((new Date()).getTime());
    }

    public Participant(Integer bibsNumber,
                       Integer createdBy, Timestamp creationDate,
                       Timestamp finishTime, Timestamp lastUpdateDate,
                       Integer lastUpdatedBy, Integer participantId,
                       Person person, Race race) {
        this.bibsNumber = bibsNumber;
        this.createdBy = createdBy;
        this.creationDate = creationDate;
        this.finishTime = finishTime;
        this.lastUpdateDate = lastUpdateDate;
        this.lastUpdatedBy = lastUpdatedBy;
        this.participantId = participantId;
        this.person = person;
        this.race = race;
    }

    public Integer getBibsNumber() {
        return bibsNumber;
    }

    public void setBibsNumber(Integer bibsNumber) {
        this.bibsNumber = bibsNumber;
    }

    public Integer getCreatedBy() {
        return createdBy;
    }

    public void setCreatedBy(Integer createdBy) {
        this.createdBy = createdBy;
    }

    public Timestamp getCreationDate() {
        return creationDate;
    }

    public void setCreationDate(Timestamp creationDate) {
        this.creationDate = creationDate;
    }

    public Timestamp getFinishTime() {
        return finishTime;
    }

    public void setFinishTime(Timestamp finishTime) {
        this.finishTime = finishTime;
    }

    public Integer getLastUpdatedBy() {
        return lastUpdatedBy;
    }

    public void setLastUpdatedBy(Integer lastUpdatedBy) {
        this.lastUpdatedBy = lastUpdatedBy;
    }

    public Timestamp getLastUpdateDate() {
        return lastUpdateDate;
    }

    public void setLastUpdateDate(Timestamp lastUpdateDate) {
        this.lastUpdateDate = lastUpdateDate;
    }

    public Integer getParticipantId() {
        return participantId;
    }

    public void setParticipantId(Integer participantId) {
        this.participantId = participantId;
    }

    public void setPerson(Person person) {
        this.person = person;
    }

    public Person getPerson() {
        return person;
    }

    public void setRace(Race race) {
        this.race = race;
    }

    public Race getRace() {
        return race;
    }

    public void setTimes(List<Time> times) {
        this.times = times;
    }

    public List<Time> getTimes() {
        return times;
    }

    public void setCategories(List<Category> categories) {
        this.categories = categories;
    }

    public List<Category> getCategories() {
        return categories;
    }
}
